package com.banyuan.array;


import java.util.Arrays;

//要实现的功能是将两个有序排列的数组中的元素有序放到另外的一个数组中
public class TwoArraysModify {
    public static void main(String[] args) {

        //定义两个有序的数组
        int[] arr1 = new int[]{1, 3, 5, 7, 9};
        int[] arr2 = new int[]{2, 4, 6, 8, 10, 12};

        //定义所需要的索引
        int index1 = 0;
        int index2 = 0;
        int index3 = 0;
        int[] arr3 = new int[arr1.length + arr2.length];

        //都有值的时候，利用index1和index2相互比较，将较大值赋给arr3[index3],同时index3自增1，且赋值的那个数组的index自增1
        while (index1 < arr1.length && index2 < arr2.length) {
            arr3[index3++] = arr1[index1] < arr2[index2] ? arr1[index1++] : arr2[index2++];
        }

        //当一个数组的值已经全部用完之后，做出判断是那个数组的值已经用完了
        while (index1 < arr1.length) {
            //arr1剩下的元素全部赋给arr3；
            arr3[index3++] = arr1[index1++];
        }

        while (index2 < arr2.length) {
            arr3[index3++] = arr2[index2++];
        }

        System.out.println(Arrays.toString(arr3));

    }
}
